# -*- coding: utf-8 -*-
'''
@Time : 2021/5/26 15:21
@description: 封装成人录取检测程序
@Author : yin
@Email : yylai24@163.com
@File : Check.py
@Project : SDUT
'''

import pandas as pd
import os
import configparser
from tqdm import tqdm

config = configparser.ConfigParser()
config.read("config.ini",encoding='utf-8')

class CheckAdult:
    def __init__(self):
        pass

    def exis_position(self,x):
        """
        :param x: 输入的字段
        :return: 返回是否存在
        """
        regex = "院校代码"
        if x.find(regex) != -1 or x.find("录取院校") != -1 or x.find("单位")!=-1:
            return True
        else:
            return False

    def split_space(self,x):
        """
        :param x:字符串
        :return:没有空格的数据
        """
        return x.replace(" ", "")

    def read_all_data(self,path):
        """
        :param path: 某一年总数据的xlsx文件路径
        :return: 该年的总数据
        """
        # 获取文件下的分表
        sheet_list = list(pd.read_excel(path, sheet_name=None))
        data = pd.DataFrame(columns=['student_name', 'student_id'])
        for i in sheet_list:
            student_part_data = pd.read_excel(path, sheet_name=i, dtype=object)
            if set(["姓名", "身份证号"]).issubset(list(student_part_data.columns)):
                student_part_data = student_part_data.loc[:, ["姓名", "身份证号"]]
                student_part_data.rename(columns={"姓名": "student_name", "身份证号": "student_id"}, inplace=True)
                data = pd.concat([data, student_part_data], ignore_index=True, axis=0)

        data['flag'] = "总表数据"
        data['student_name'] = data['student_name'].astype(str).apply(self.split_space)
        data['student_id'] = data['student_id'].astype(str).apply(self.split_space)
        return data

    def get_ocr_data(self,path):
        """
        :param path: 分表文件路径
        :return: 处理后的分表数据
        """
        # 读取文件信息
        ocr_data = pd.read_excel(path)
        column_name = list(ocr_data.columns)[0]
        flag = self.exis_position(column_name)
        if flag:
            ocr_data_info = pd.read_excel(path, skiprows=[0, 1, 2, 3],dtype=object)
            if set(['姓名', '身份 证号']).issubset(list(ocr_data_info.columns)):
                ocr = ocr_data_info.loc[:, ['姓名', '身份 证号']]
                ocr.rename(columns={"姓名": "student_name", "身份 证号": "student_id"}, inplace=True)
                ocr.dropna(axis=0, inplace=True)
                ocr['flag'] = '分表数据'
                ocr['student_name'] = ocr['student_name'].astype(str).apply(self.split_space)
                ocr['student_id'] = ocr['student_id'].astype(str).apply(self.split_space)
                return ocr
            if set(['姓名', '身份证号']).issubset(list(ocr_data_info.columns)):
                ocr = ocr_data_info.loc[:, ['姓名', '身份证号']]
                ocr.rename(columns={"姓名": "student_name", "身份证号": "student_id"}, inplace=True)
                ocr.dropna(axis=0, inplace=True)
                ocr['flag'] = '分表数据'
                ocr['student_name'] = ocr['student_name'].astype(str).apply(self.split_space)
                ocr['student_id'] = ocr['student_id'].astype(str).apply(self.split_space)
                return ocr

            # else:
            #     print(path)
            #     print("列出错")
            #     return pd.DataFrame(columns=['student_id','student_name'])
        else:
            if column_name.find("层次")==-1:
                ocr_data_info = pd.read_excel(path, skipfooter=4,dtype=object)
                if set(['姓名', '身份 证号']).issubset(list(ocr_data_info.columns)):
                    ocr = ocr_data_info.loc[:, ['姓名', '身份 证号']]
                    ocr.rename(columns={"姓名": "student_name", "身份 证号": "student_id"}, inplace=True)
                    ocr.dropna(axis=0, inplace=True)
                    ocr['flag'] = '分表数据'
                    ocr['student_name'] = ocr['student_name'].astype(str).apply(self.split_space)
                    ocr['student_id'] = ocr['student_id'].astype(str).apply(self.split_space)
                    return ocr
                if set(['姓名', '身份证号']).issubset(list(ocr_data_info.columns)):
                    ocr = ocr_data_info.loc[:, ['姓名', '身份证号']]
                    ocr.rename(columns={"姓名": "student_name", "身份证号": "student_id"}, inplace=True)
                    ocr.dropna(axis=0, inplace=True)
                    ocr['flag'] = '分表数据'
                    ocr['student_name'] = ocr['student_name'].astype(str).apply(self.split_space)
                    ocr['student_id'] = ocr['student_id'].astype(str).apply(self.split_space)
                    return ocr
            else:
                ocr_data_info = pd.read_excel(path, skiprows=[0, 1], dtype=object)
                if set(['姓名', '身份 证号']).issubset(list(ocr_data_info.columns)):
                    ocr = ocr_data_info.loc[:, ['姓名', '身份 证号']]
                    ocr.rename(columns={"姓名": "student_name", "身份 证号": "student_id"}, inplace=True)
                    ocr.dropna(axis=0, inplace=True)
                    ocr['flag'] = '分表数据'
                    ocr['student_name'] = ocr['student_name'].astype(str).apply(self.split_space)
                    ocr['student_id'] = ocr['student_id'].astype(str).apply(self.split_space)
                    return ocr
                if set(['姓名', '身份证号']).issubset(list(ocr_data_info.columns)):
                    ocr = ocr_data_info.loc[:, ['姓名', '身份证号']]
                    ocr.rename(columns={"姓名": "student_name", "身份证号": "student_id"}, inplace=True)
                    ocr.dropna(axis=0, inplace=True)
                    ocr['flag'] = '分表数据'
                    ocr['student_name'] = ocr['student_name'].astype(str).apply(self.split_space)
                    ocr['student_id'] = ocr['student_id'].astype(str).apply(self.split_space)
                    return ocr
            # else:
            #     print(path)
            #     print("列出错")
            #     return pd.DataFrame(columns=['student_id', 'student_name'])

    def check_data(self,full_data, check_data):
        """
        :param full_data: 总数据
        :param check_data: 分表数据
        :return: 不正确的数据
        """
        # 获得分表中student_id


        student_id_list = check_data['student_id'].tolist()
        # 从总表中获取student_id
        # print(full_data['student_id'].isin(student_id_list).value_counts())
        get_full_data = full_data[full_data['student_id'].isin(student_id_list)]
        data = pd.concat([check_data, get_full_data], ignore_index=True, axis=0)
        data.drop_duplicates(subset=["student_name", "student_id"], keep=False, inplace=True)
        return data

    def save_error(self, path, data):
        data.dropna(axis=0, inplace=True)
        if not data.empty:
            data = data.sort_values(by=['student_name', 'student_id'])
            data.to_excel(path, index=None)

    def mkdir_path(self,path):
        """
        :param path: 文件夹的创建
        """
        if not os.path.exists(path):
            os.mkdir(path)

    def get_all_excel(self,rootdir):
        """
        获取某文件夹底下所有Excel表格的路径
        :param rootdir: 文件夹路径
        :return: Excel表格路径列表
        """
        excel_path_list = []
        for root, dirs, files in os.walk(rootdir):
            for file in files:
                if file.endswith(".xlsx"):
                    file_path = os.path.join(root, file)
                    excel_path_list.append(file_path)
        return excel_path_list

    def run(self):
        """
        执行程序
        :return:
        """
        all_data = self.read_all_data(config['path']['allInfoTablePath'])
        excelPathList = self.get_all_excel(config['path']['infoTablesDir'])
        self.resultPath = config['path']['resultPath']
        self.mkdir_path(self.resultPath)
        for filePath in tqdm(excelPathList):
            try:
                ocr_data = self.get_ocr_data(filePath)
                error = self.check_data(all_data, ocr_data)
                self.save_error("{}/{}.xlsx".format(self.resultPath, os.path.basename(filePath)),error)
            except Exception as e:
                with open("{}/error.log".format(self.resultPath), "a+", encoding="utf-8") as tf:
                    tf.write(str(e) + "\t" + filePath + "\n")

